package com.geezdata.cps.base.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.geezdata.cps.base.constant.Constants;


@Configuration
@ConditionalOnClass({JSON.class})
public class FastJsonConfig {
	@Bean
    public HttpMessageConverters fastJsonHttpMessageConverters(){
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        
        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(supportedMediaTypes);
        
        com.alibaba.fastjson.support.config.FastJsonConfig fastJsonConfig = 
        		new com.alibaba.fastjson.support.config.FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
        		SerializerFeature.PrettyFormat,
        		SerializerFeature.WriteMapNullValue,
        		SerializerFeature.WriteNullStringAsEmpty,
        		SerializerFeature.WriteNullListAsEmpty,
        		SerializerFeature.WriteDateUseDateFormat
        );
        fastJsonConfig.setCharset(Charset.forName(Constants.APPLICATION_CHARSET));
        fastJsonConfig.setDateFormat(Constants.APPLICATION_DATE_FORMAT);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }
}
